package com.culturesalon.dao;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.culturesalon.connectionfactory.ConnectionFactory;
import com.culturesalon.dto.Goods;
import com.culturesalon.dto.Orders;
import com.culturesalon.dto.Shopcart;
import com.culturesalon.dto.User;
import com.culturesalon.util.OrderState;
import com.culturesalon.util.PaymentWay;
import com.mysql.jdbc.Connection;

public class ShopcartDAOImpl implements ShopcartDAO {
	

	@Override
	public Shopcart get(Integer id) {
		PreparedStatement stmt=null;
		ResultSet rs=null;
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT id,goodsName,retailPrice,amount,totaoPrice,goodsid,userId,goodsPictureUrl");
		
		sql.append(" FROM T_SHOPCART ");
		sql.append(" WHERE ID=? ");
		Connection conn = (Connection) ConnectionFactory.getConnection();
		Shopcart shopcart = null;
		try {
			stmt=conn.prepareStatement(sql.toString());
			stmt.setInt(1, id);
			rs=stmt.executeQuery();
			if(rs.next()){
				shopcart=new Shopcart();
				shopcart.setId(rs.getInt("id"));
				shopcart.setGoodsName(rs.getString("goodsName"));
				shopcart.setRetailPrice(rs.getDouble("retailPrice"));
				shopcart.setAmount(rs.getInt("amount"));
				shopcart.setTotaoPrice(rs.getDouble("totaoPrice"));
				shopcart.setGoodsid(rs.getInt("goodsid"));
				shopcart.setUserId(rs.getInt("userId"));
				shopcart.setGoodsPictureUrl(rs.getString("goodsPictureUrl"));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally{
			ConnectionFactory.close(conn, rs, stmt);
		}
		return shopcart;
	 }

	@Override
	public Serializable save(Shopcart shopcart) {
		java.sql.Connection conn=null;
		PreparedStatement pstm=null;
		StringBuffer sql=new StringBuffer();
		sql.append(" INSERT INTO T_SHOPCART( ");
		sql.append("goodsName,retailPrice,amount,totaoPrice,goodsid,userId,goodsPictureUrl)");
		
		sql.append(" VALUES(?,?,?,?,?,?,?) ");
		conn=ConnectionFactory.getConnection();
		int result=0;
		try {
			pstm=conn.prepareStatement(sql.toString());
			pstm.setString(1, shopcart.getGoodsName());
			pstm.setDouble(2, shopcart.getRetailPrice());
			pstm.setInt(3, shopcart.getAmount());
			pstm.setDouble(4, shopcart.getTotaoPrice());
			pstm.setInt(5, shopcart.getGoodsid());
			pstm.setInt(6,shopcart.getUserId());
			pstm.setString(7, shopcart.getGoodsPictureUrl());
			result=pstm.executeUpdate();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}finally{
			ConnectionFactory.close(conn, null, pstm);
		}
		return result;
		
	}

	@Override
	public void update(Shopcart shopcart) {
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement pstm=null;
		StringBuffer sql=new StringBuffer();
		sql.append(" UPDATE T_SHOPCART SET GOODSNAME=?,");
		sql.append("RetailPrice=?,Amount=?,");
		sql.append("TotaoPrice=?,Goodsid=?,UserId=?,goodsPictureUrl=?");
		sql.append(" WHERE ID=?");
		try {
			conn=(Connection) ConnectionFactory.getConnection();
			pstm=conn.prepareStatement(sql.toString());
			pstm.setString(1, shopcart.getGoodsName());
			pstm.setDouble(2, shopcart.getRetailPrice());
			pstm.setInt(3, shopcart.getAmount());
			pstm.setDouble(4, shopcart.getTotaoPrice());
			pstm.setInt(5, shopcart.getGoodsid());
			pstm.setInt(6, shopcart.getUserId());
			pstm.setString(7, shopcart.getGoodsPictureUrl());
			pstm.setInt(8, shopcart.getId());
			pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();}
		finally{
			ConnectionFactory.close(conn, null, pstm);
		}
		
	}

	@Override
	public Serializable delete(Serializable id) {
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement pstm=null;
		StringBuffer sql= new StringBuffer();
		sql.append( " DELETE FROM T_SHOPCART WHERE id=?" );
		conn=(Connection) ConnectionFactory.getConnection();
		try {
			pstm=conn.prepareStatement(sql.toString());
			pstm.setInt(1,Integer.parseInt((String) id)) ;
			
			int records=pstm.executeUpdate();
			return records;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}finally{
			ConnectionFactory.close(conn, null,pstm);
		}
	}

	@Override
	public List<Shopcart> findAll() {
		// TODO Auto-generated method stub
		List<Shopcart> list=new ArrayList<Shopcart>();
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT * FROM T_SHOPCART");
		conn=(Connection) ConnectionFactory.getConnection();
		Shopcart shopcart=null;
		try {
			pstm=conn.prepareStatement(sql.toString());
			rs=pstm.executeQuery();
			while(rs.next()){
				shopcart=new Shopcart();
				shopcart.setId(rs.getInt("id"));
				shopcart.setGoodsName(rs.getString("goodsName"));
				shopcart.setRetailPrice(rs.getDouble("retailPrice"));
				shopcart.setAmount(rs.getInt("amount"));
				shopcart.setTotaoPrice(rs.getDouble("TotaoPrice"));
				shopcart.setGoodsid(rs.getInt("goodsid"));
				shopcart.setUserId(rs.getInt("userId"));
				shopcart.setGoodsPictureUrl(rs.getString("goodsPictureUrl"));
				list.add(shopcart);
				
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally{
			ConnectionFactory.close(conn, rs, pstm);
		}
	}

	public List<Shopcart> findAllByUser(User user) {
		// TODO Auto-generated method stub
		List<Shopcart> list = new ArrayList<Shopcart>();
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM T_SHOPCART WHERE USERID=? ");
		conn=(Connection) ConnectionFactory.getConnection();
		Shopcart shopcart = null;
		try {
			pstm = conn.prepareStatement(sql.toString());
			pstm.setInt(1, user.getId());
			rs = pstm.executeQuery();
			while (rs.next()) {
				shopcart=new Shopcart();
				shopcart.setId(rs.getInt("id"));
				shopcart.setGoodsName(rs.getString("goodsName"));
				shopcart.setRetailPrice(rs.getDouble("retailPrice"));
				shopcart.setAmount(rs.getInt("amount"));
				shopcart.setTotaoPrice(rs.getDouble("TotaoPrice"));
				shopcart.setGoodsid(rs.getInt("goodsid"));
				shopcart.setUserId(rs.getInt("userId"));
				shopcart.setGoodsPictureUrl(rs.getString("goodsPictureUrl"));
				list.add(shopcart);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			ConnectionFactory.close(conn, rs, pstm);

		}
	}
}
